Algorithmic trading with Python and Sentiment Analysis Tutorial error

by: ABQJuan, 9 years ago


Hi Harrison, I am coding along with your Quantopian series and got stuck on video 10 in the series, the one where you code the trading logic for the Sentdex data example. When I run a full backtest I get an timeout error from the fetch_csv function: "TimeoutException: Fetcher data not processed in 360 seconds
There was a runtime error on line 13."

I think that this is odd, considering I get output from the log.info function call from within the fetch_csv method with the pre_func attribute. I have checked the code and it appears to be exact with yours in the video.

Any thoughts would be appreciated. Thanks!



You must be logged in to post. Please login or register an account.



# Put any initialization logic here.  The context object will be passed to
# the other methods in your algorithm.
def preview(df):
    log.info(df.head())
    return df

def initialize(context):
    context.investment_size = (context.portfolio.cash / 10.0)
    context.stop_loss_pct = 0.995
    
    set_symbol_lookup_date('2012-10-01')

    fetch_csv('http://sentdex.com/api/finance/sentiment-signals/sample/', pre_func = preview)
    
    context.stocks = symbols('AAPL', 'MCD', 'FB', 'GME', 'INTC', 'SBUX', 'T', 'MGM', 'SHLD', 'NKE', 'NFLX', 'PFE', 'GS', 'TGT', 'NOK', 'SNE', 'TXN', 'JNJ', 'KO', 'VZ', 'XOM', 'WMT', 'MCO', 'TWTR', 'URBN', 'MCP', 'MSFT', 'HD', 'KSS', 'AMZN', 'S', 'BA', 'F', 'JPM', 'QCOM', 'TSLA', 'YHOO', 'BBRY', 'GM', 'IBM', 'C', 'ZNGA', 'BAC', 'DIS', 'SCHW', 'UA', 'CSCO', 'ORCL', 'SYMC', 'WFC', 'TM', 'EBAY', 'SCHL', 'MS', 'NDAQ', 'TIF', 'AIG', 'DAL', 'JCP', 'MRK', 'CA', 'SIRI', 'AMD', 'CVX', 'FSLR', 'LMT', 'P', 'CBS', 'TWX', 'PEP', 'LNKD', 'CMG', 'NVDA', 'BBY', 'TWC', 'M', 'RHT', 'ACN', 'CRM', 'PETS', 'CELG', 'BLK', 'GD', 'DOW', 'YUM', 'GE', 'MA', 'DTV', 'DDD', 'CAT', 'FDX', 'GRPN', 'BK', 'GILD', 'V', 'DUK', 'FFIV', 'WFM', 'CVS', 'UNH', 'LUV', 'CBG', 'AFL', 'CHK', 'BRCM', 'HPQ', 'LULU', 'ATVI', 'RTN', 'EMC', 'NOC', 'MAR', 'X', 'BMY', 'LOW', 'COST', 'HON', 'SPLS', 'BKS', 'AA', 'AXP', 'AMGN', 'GPS', 'MDT', 'LLY', 'CME', 'MON', 'WWWW', 'MU', 'DG', 'TRIP', 'HAL', 'COH', 'WYNN', 'PCLN', 'HTZ', 'CLF', 'DD', 'ACI', 'FCX', 'AON', 'GMCR', 'CSX', 'ADBE', 'PRU', 'PG', 'MYL', 'STT', 'PPG', 'EXPE', 'KORS', 'JNPR', 'UTX', 'HOT', 'SNDK', 'CCL', 'DRI', 'BIIB', 'MHFI', 'BBT', 'APA', 'A', 'TDC', 'ANF', 'MTB', 'PPL', 'ABT', 'GNW', 'KMI', 'MET', 'FE', 'DVA', 'ETFC', 'GLW', 'NRG', 'INTU', 'KR', 'ARNA', 'VALE', 'MSI', 'EOG', 'AET', 'MAT', 'HST', 'COP', 'MO', 'IVZ', 'HUM', 'NUE', 'CI')
      # Will be called on every trade event for the securities you specify.

def handle_data(context, data):
  
    cash = context.portfolio.cash
    try:
        for s in data:
            if 'sentiment_signal' in data[s]:
                sentiment = data[s]['sentiment_signal']
                current_position = context.portfolio.positions[s].amount
                current_price = data[s].price
                
                if (sentiment > 5) and (current_position == 0):
                    if cash > context.investment_size:
                        order_value(s, context.investment_size, style = StopOrder(context.stop_loss_pct*current_price))
                        cash -= context.investment_size
                                    
                elif (sentiment <= -1) and (current_position >0):
                    order_target(s, 0)
                    
                    
                
        
    except Exception as e:
        print(str(e))

-ABQJuan 9 years ago

You must be logged in to post. Please login or register an account.

Yep, I know what you're talking about. Equally perplexed by it. The data is indeed clearly there (as noted by the fact you can output it), but for some reason is timing out. New issue that has crept up. I tried making the file much smaller, still get the error. Something on their end it seems. It usually runs at least once out of a handful of tries. You can just skip that part though if you don't want to keep trying. We don't continue using the fetcher, luckily.

-Harrison 9 years ago

You must be logged in to post. Please login or register an account.


Ok thanks. I know this is somewhat deprecated stuff since the rollout of the pipeline api. But I thought I would gain some insight and better appreciation of pipeline by going through the tutorials. Which are great, BTW.

-ABQJuan 9 years ago

You must be logged in to post. Please login or register an account.